package com.bltec.emergency.controller;

import com.bltec.emergency.common.Constant;
import com.bltec.emergency.common.MySQLDatabaseBackup;
import com.bltec.emergency.common.ResponseJson;
import com.bltec.emergency.utils.DateUtil;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;

@RestController
public class BackupController
{
    @RequiresRoles("superManger")
    @RequestMapping("/runBackup")
    public ResponseJson runBackup() {
        String fileName= DateUtil.now(new SimpleDateFormat("yyyy-MM-hh-HH-mm-ss"))+".sql";
        String dirPath ="D:\\APPDATA\\resource\\upload";
        try {
            if (MySQLDatabaseBackup.exportDatabaseTool("47.92.226.52", "root", "BENLIU@888", dirPath, fileName, "emergency")) {
                //String CMDIn= "mysqldump -h47.92.226.52 -P3305 -uroot -p --databases emergency >D:/software/emergency.sql";
                return new ResponseJson(Constant.OK,"备份成功!----位置："+dirPath+"----文件名:"+fileName);
            }
            else {
                return  new ResponseJson(Constant.FAIL,"备份失败");
            }
        } catch (InterruptedException e) {
            return new ResponseJson(Constant.FAIL,"备份失败:"+e.getMessage());
        }

    }
}
